// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

package ClientSide;

import ocsf.client.*;
import Common.*;
//import File.CMyFile;

import java.io.*;
import java.util.HashMap;
import java.util.Vector;
import ServerSide.*;
/**
 * This class overrides some of the methods defined in the abstract
 * superclass in order to give more functionality to the client.
 *
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;
 * @author Fran&ccedil;ois B&eacute;langer
 * @version July 2000
 */
public class ChatClient extends AbstractClient
{
  //Instance variables **********************************************
  
  /**
   * The interface type variable.  It allows the implementation of 
   * the display method in the client.
   */
  ChatIF clientUI; 

  
  //Constructors ****************************************************
  
  /**
   * Constructs an instance of the chat client.
   *
   * @param host The server to connect to.
   * @param port The port number to connect on.
   * @param clientUI The interface type variable.
   */
  
  public ChatClient(String host, int port, ChatIF clientUI) 
    throws IOException 
  {
    super(host, port); //Call the superclass constructor
    this.clientUI = clientUI;
    openConnection();
  }

  
  //Instance methods ************************************************
    
  /**
   * This method handles all data that comes in from the server.
   *
   * @param msg The message from the server.
   */
  public void handleMessageFromServer(Object msg) 
  {
	  String[] rs=null;
	  int i;
	 

  if (msg instanceof HashMap){
		  ((ClientConsole)clientUI).NewAssignmentScreenData((HashMap) msg);
	  System.out.println("\n In handle msg from server the msg is HHHHHHHHAAAASHHHHHH");
  	}

	  if (msg instanceof Vector){
		  Vector<String[]> result=(Vector<String[]>)msg;
		  String[] identity=result.get(result.size()-1);
		  if(identity[0].startsWith("CoursesOfStudent"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			  System.out.println("\n In handle msg from server the msg is Courses");
			  ((ClientConsole)clientUI).updateCoursesInfoStudent(msg);
		  
			  
		  }
		  else  if(identity[0].startsWith("ChildrenOfParent"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			  identity=result.get(result.size()-2);
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateChildrenToParent(msg);
		  }
		  else  if(identity[0].startsWith("ForParentCoursesOfStudent"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			 
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateChildrenCoursesToParent(msg);
		  }
		  else if (identity[0].startsWith("ForPrincipleCoursesOfStudent"))
		  {
			  System.out.println("\n In handle msg from server ForPrincipleCoursesOfStudent");
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			 
			 ((ClientConsole)clientUI).updateCoursesForPrinciple(msg);
			  
		  }
		
		  
		  else if (identity[0].startsWith("PersonalDataForStudent"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			((ClientConsole)clientUI).updatePDataStudent(msg);
			  
		  }
		  else if (identity[0].startsWith("PersonalDataForParent"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			((ClientConsole)clientUI).updateChildDataToParent(msg);
		  }
		  else if (identity[0].startsWith("PersonalDataForPrinciple"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			((ClientConsole)clientUI).updateStudentsDataToPrinciple(msg);
		  }
		  else if (identity[0].startsWith("ClassesQueryForAdmin"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  result.remove(result.size()-1);
			  ((ClientConsole)clientUI).updateClassesAdmin(msg);
		  }
		  else if (identity[0].startsWith("StudUnatQuery"))
		  {
			  System.out.println("\n identity "+identity[0]+ result.size());
			  
			 if(!(result.get(result.size()-1)[1]==null))
				 
				 	msg=(Object)(new Vector<String[]>());
			   
			 ((ClientConsole)clientUI).updateUnattStud(msg);
		  }
		  else if (identity[0].startsWith("CoursesAdminQuery"))
		  {
			  System.out.println("\n identity "+identity[0]); 
			  if(!(result.get(result.size()-1)[1]==null))
				  msg=(Object)(new Vector<String[]>());
			  ((ClientConsole)clientUI).updateRawCoursesToAdmin(msg);
		  }
		  else if (identity[0].startsWith("TeachersQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateTeachersToAdmin(msg);
		  }
		  else if (identity[0].startsWith("StudentQueryForEx"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateStudentsForEx(msg);
		  }
		  else if (identity[0].startsWith("ForAdminCoursesOfStudent"))
		  {
			  System.out.println("\n identity "+identity[0]); 
			  
			  ((ClientConsole)clientUI).updateStudentCoursesForEx(msg);
		  }
		  else if (identity[0].startsWith("StudentCoursesRemoveExQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			 
			  ((ClientConsole)clientUI).updateStudentCoursesForRemEx(msg);
		  }
		  else if (identity[0].startsWith("StudentCoursesAddExQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			 
			  ((ClientConsole)clientUI).updateStudentCoursesForAddEx(msg);
		  }
		  
		  else if (identity[0].startsWith("CoursesOfClassesAdminQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
				 
			  ((ClientConsole)clientUI).updateClassesCoursesForEx(msg);
		  }
		  else if (identity[0].startsWith("ParentQueryForManagment"))
		  {
			  System.out.println("\n identity "+identity[0]);
				 
			  ((ClientConsole)clientUI).updateParentsToManagment(msg);
		  }
		  else if (identity[0].startsWith("CoursesOfStudOfClassQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
				 
			  ((ClientConsole)clientUI).updateStudCoursesForAtt(msg);
		  }
		  else if (identity[0].startsWith("CoursesWithPreCoursesQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
				 
			  ((ClientConsole)clientUI).updatePreCoursesForAtt(msg);
		  }
		  else if (identity[0].startsWith("StudentwithClassQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
				 
			  ((ClientConsole)clientUI).updateStudentWithClassesForAtt(msg);
		  }
		  else if (identity[0].startsWith("AttributeStudentsToCourse"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).returnResultAttStudAndClassToCourse(msg);
			  
		  }
		  else if (identity[0].startsWith("UpdateLockToCurrentSemester"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).returnAppToLockSemester(msg);
		  }
		  else if ((identity[0].startsWith("CreateExReqRemoveStudFromCourse"))||(identity[0].startsWith("CreateExReqAddStudToCourse"))||(identity[0].startsWith("CreateExReqTeacherReplacement")))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).returnAppToCreateExReqStudRem(msg);
		  }
		  else if (identity[0].startsWith("DataForHistBetweenTeachersInChosenClass"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateDataHistBetweenTeachersInChosenClass(msg);
		  }
		  else if (identity[0].startsWith("DataForHistBetweenClassesToChosenTeacher"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateDataForHistBetweenClassesToChosenTeacher(msg);
		  }
		  else if (identity[0].startsWith("DataForHistBetweenCoursesToChosenClass"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateDataForHistBetweenCoursesToChosenClass(msg);
		  }
		  else if (identity[0].startsWith("CoursesOfClassQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateCoursesOfClass(msg);
		  }
		  else if (identity[0].startsWith("InsertStudentToClass"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).returnAppInsertStudentToClass(msg);
		  }
		  else if ((identity[0].startsWith("SetDesExReqAddStudToCourse"))||(identity[0].startsWith("SetDesExReqRemovalStudToCourse"))||(identity[0].startsWith("SetDesExReqReplaceTeacher")))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateDesExReqAddStudToCourse();
		  }
		  else if (identity[0].startsWith("ExReqTeacherRepQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateExReqTeacherRepQuery(msg);
		  }
		  else if (identity[0].startsWith("ExReqStudAddQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateExReqStudAddQuery(msg);
		  }
		  else if (identity[0].startsWith("ExReqStudRemQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateExReqStudRemQuery(msg);
		  }
		  else if (identity[0].startsWith("CoursesAdminForExQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateCoursesAdminForExQuery(msg);
		  }
		  else if (identity[0].startsWith("TeachersForHistQuery"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateTeachersForHisPrinciple(msg);
		  }
		  else if (identity[0].startsWith("CreateNewClass"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateApproveToNewClass();
		  }
		  else if (identity[0].startsWith("CreateNewSemester"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateApproveToNewSemester();
		  }
		  else if (identity[0].startsWith("ParentBlockForManagment"))
		  {
			  System.out.println("\n identity "+identity[0]);
			  ((ClientConsole)clientUI).updateApproveToParentalBlock();
		  }
		 /* if(identity[0].startsWith("CoursesOfStudent"))
		  {
			  result.remove(0);
			  System.out.println("\n In handle msg from server the msg is Courses");
			  ((ClientConsole)clientUI).updateCoursesInfoStudent(msg);
		  }*/
		  	/*Vector<String[]> result=(Vector<String[]>)msg;
		  	for (i=1; i<result.size(); i++){
				String[] array = (String[])result.get(i);
				for (int j = 0; j < array.length; j++);*/
		  
				
		  
			/*System.out.println("\nThe result of the query is:\n");
			
			String[] headlines = (String[])result.get(0);  // gets the headlines
			
			for (i=0; i<headlines.length; i++)
				System.out.format("%-22s\t", headlines[i]);
			System.out.println();
			
			for (i=1; i<result.size(); i++){
				String[] array = (String[])result.get(i);
		        for (int j = 0; j < array.length; j++)
					//System.out.print(array[j]+"\t\t");
		        	System.out.format("%-22s\t", array[j]);
		        System.out.print("\n");
			}
			System.out.println("\nPlease enter your query:");*/
	  }
	  else if (msg instanceof Boolean)
	  {
		  System.out.println("In handle msg from server the status is"+msg.toString()); 
		  ((ClientConsole)clientUI).updateDoneStatus(msg);
	  }
		

	  else 
		{
		  rs=(String[])msg;	
		  /////////////////////////////////
		  
		  
		 //////////////////////////////////
		  System.out.println("In handle msg from server the msg is" + rs[0]+" "+rs[1]+" "+rs[2]+" "+rs[3]);//0-id,1-connection status,2-user type, 3-login status(ok\failed)
		  clientUI.display(rs[0]+" "+rs[1]+" "+rs[2]+" "+rs[3]);
				  }
	 }
	  		
	  		

  /**
   * This method handles all data coming from the UI            
   *
   * @param message The message from the UI.    
   */
  public void handleMessageFromClientUI(String message)
  {
    try
    {
    	System.out.println("\nIn  handleMessageFromClientUI1 "+message+"\n");
  
    	sendToServer(message);
    	
    }
    catch(IOException e)
    {
      clientUI.display
        ("Could not send message to server.  Terminating client.");
      quit();
    }
  }
  
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //getting files from User:
//  public void handleFileFromUI(CMyFile msg){//String name,String filePath){
//	  CMyFile msg= new CMyFile(name);
//	  String LocalfilePath=filePath;
//
//	  try{
//		  	System.out.println("in Chat CLient "+msg.getFileName());
//		  		System.out.println("aaaaa"); 
//		  		File newFile = new File (LocalfilePath);
//		System.out.println("bbbbbbb");      		      
//		      byte [] mybytearray  = new byte [(int)newFile.length()];
//		      System.out.println("cccc"); 
//		      FileInputStream fis = new FileInputStream(newFile);
//		      BufferedInputStream bis = new BufferedInputStream(fis);			  
//		      System.out.println("dddddddddddd"); 
//		      msg.initArray(mybytearray.length);
//		      msg.setSize(mybytearray.length);
//		      System.out.println("saaaaa"); 
//		      bis.read(msg.getMybytearray(),0,mybytearray.length);
//		      System.out.println("bassasas"); 
//		      sendToServer(msg);		  
//		      System.out.println("saaaaaaaaaaaaabbbbb"); 
//		    }
//		catch (Exception e) {
//			System.out.println("Error send (Files)msg) to Server");
//		}
//	}
//  
//  
   /////////////////////////////////////////////////////////////////////////////////////////////////
  public void handleFileFromUI(File msg){ 
	  try{
		  	System.out.println("in Chat CLient paht is: "+msg.getPath()+"name is : "+msg.getName());  
	

      sendToServer(msg);		  
      System.out.println("message been sent to server"); 
    }
catch (Exception e) {
	System.out.println("Error send (Files)msg) to Server");
}
}

  
  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
  
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  /**
   * This method terminates the client.
   */
  public void quit()
  {
    try
    {
      closeConnection();
    }
    catch(IOException e) {}
    System.exit(0);
  }
  
  
}
//End of ChatClient class
